package cn.diancan.util;

 
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader; 
import java.beans.PropertyDescriptor;
import java.io.File;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class SettingUtils {



    private static Map<String,Setting> map = new HashMap<String, Setting>();

    private static String[] names ={ "common" ,"lmct","nryb"};
    private static Long lastModified=0L;
    // 配置文件名
    private static String filename = "setting_site.xml";
    
    static{
        load();
    }

    public static Map<String,Setting> getAll(){
	load();
        return map;
    }

    public static Setting getSetting(String key){ 
        return getAll().get(key);
    }
    
    public static Setting getCommonSetting(){ 
        return getAll().get("common");
    }

    public static Setting getLmctSetting(){ 
        return getAll().get("lmct");
    }

    public static Setting getNrybSetting(){ 
        return getAll().get("nryb");
    }

    public static void load(){
        load(filename);
    }
  
    private static synchronized void load( String filename) {
        try {  
            File file = new File(new SettingUtils().getClass().getClassLoader().getResource(filename).getPath());
            if (!file.exists()) {  
                System.out.println("  Error : Config file doesn't exist!");  
                System.exit(1);  
            }
            if(lastModified.equals(file.lastModified())){
            	return  ;
            }
           
            Document   document = (new SAXReader()).read(file);  
            for (int i=0;i<names.length;i++){
                String name = names[i];
                List<Element> list = document.selectNodes("/website/"+name+"/setting");
                Setting setting =null;
                if(null == map.get(name)){
                    setting = new Setting();
                    map.put(name,setting);
                }else{
                    setting = map.get(name);
                }

                Iterator<Element> iterator = list.iterator();
                while(iterator.hasNext()){
                    Element element1 = iterator.next();
                    String sName = element1.attributeValue("name");
                    if(sName.equals("isReviewEnabled")){

                    }
                    String sValue = element1.attributeValue("value");
                    PropertyDescriptor proDescriptor = new PropertyDescriptor(sName, Setting.class);
                    Method methodSet = proDescriptor.getWriteMethod();
                    if(null== methodSet){
                        System.out.println(sName+" is no exists");
                        continue;
                    }
                    methodSet.invoke(setting, sValue);
                }
            }

            lastModified = file.lastModified();
        } catch (Exception ex) {  
            System.out.println("Error : " + ex.toString());  
        }
        return  ;
    }

     



     
    
}
